<template>
  <div>
    <el-button class="btn" type="primary" size="mini" @click="addPage">添加页面权限</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="code" label="标识" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleAdd(scope.row.id)">添加</el-button>
          <el-button type="text" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add ref="add" @refresh="loadTableData" />
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { dataToTree } from '@/utils/tools'
import Add from './components/add.vue'
// import { modelConfirmClose } from '@/mixins'
export default {
  components: {
    Add
  },
  data() {
    return {
      tableData: []
    }
  },
  created() {
    this.loadTableData()
  },
  methods: {
    loadTableData() {
      getPermissionList().then(res => {
        const data = res.data
        const treeData = dataToTree(data)
        this.tableData = treeData
        console.log(this.tableData)
      }).catch(err => {
        console.log(err)
      })
    },
    addPage() {
      this.$refs.add.open(1, 0)
    },
    handleAdd(id) {
      // 由于用户此刻点击的是添加按钮操作权限，所以type值肯定是2，所以把2传进去
      this.$refs.add.open(2, id)
    },
    handleEdit(row) {
      // console.log(`编辑 ${index}`)
      this.$refs.add.edit(row)
    },
    handleDelete(index) {
      console.log(`删除 ${index}`)
    }
  }
}
</script>
<style scoped>
.btn {
  margin: 10px;
}
</style>
